/**
 * @file
 * @brief   游戏窗口类定义文件。
 * @author  赵一
 * @date    2012/01/07
 * @version 0.1
 */

/**
 * @package com.happybluefin.tapburstfree.window 窗口包。
 */
package com.happybluefin.tapburstfree.window;

import org.cocos2dx.lib.Cocos2dxActivity;

import com.happybluefin.android.framework.dialog.MessageDialog;
import com.happybluefin.android.framework.utility.thirdpart.advertisement.Banner.GoogleAdmobSDK;
import com.happybluefin.android.framework.utility.thirdpart.rankinglist.ScoreloopSDK;
import com.happybluefin.android.framework.utility.system.Comment;
import com.happybluefin.android.framework.utility.system.Network;
import com.happybluefin.android.framework.utility.system.Share;
import com.happybluefin.android.framework.utility.system.Utility;
import com.happybluefin.android.framework.utility.thirdpart.statistics.UmengSDK;
import com.happybluefin.tapburstfree.R;
import com.happybluefin.tapburstfree.message.Define;
import com.happybluefin.tapburstfree.application.TapBurstFreeApplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.Toast;

/**
 * @class  TapBurstFreeWindow
 * @brief  游戏窗口类。
 * @author 赵一
 */
public class TapBurstFreeWindow extends Cocos2dxActivity {
	/**
	 * @brief     窗口建立函数。
	 * @author    赵一
	 * @param[in] savedInstanceState 保存状态。
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//Log.d(TAG, "onCreate() start");

		//初始化控件
		_initView();

		//初始化数据统计
		_initStatistics();

		//初始化横幅广告
		_initBanner();

		//初始化推送广告
		_initPush();

		//Log.d(TAG, "onCreate() end");
	}

	/**
	 * @brief  窗口释放函数。
	 * @author 赵一
	 */
	@Override
	public void onDestroy() {
		super.onDestroy();
		//Log.d(TAG, "onDestroy() start");

		// destory google admob
		if (TapBurstFreeApplication.mIsAdBannerEnable == 1) {
			GoogleAdmobSDK.destoryBanner();
		}

		// destory push
		if (TapBurstFreeApplication.mIsAdPushEnable == 1) {
		}

		// unregist receiver
		unregisterReceiver(mReceiver);

		//Log.d(TAG, "onDestroy() end");
	}

	/**
	 * @brief  窗口暂停函数。
	 * @author 赵一
	 */
	@Override
	protected void onPause() {
		//Log.d(TAG, "onPause() start");

		super.onPause();

		UmengSDK.onPause(this);

		//Log.d(TAG, "onPause() end");
	}

	/**
	 * @brief  窗口再开始函数。
	 * @author 赵一
	 */
	@Override
	protected void onResume() {
		//Log.d(TAG, "onResume() start");

		super.onResume();

		UmengSDK.onResume(this);

		//Log.d(TAG, "onResume() end");
	}

	/**
	 * @brief     按键处理函数。
	 * @author    赵一
	 * @param[in] keyCode 按键键值。
	 * @param[in] event   事件。
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		//Log.d(TAG, "onKeyDown() start");
		boolean result = false;

		if(keyCode==KeyEvent.KEYCODE_BACK){
			_exitApplication();

			result = true;
		}
		else {
			result = super.onKeyDown(keyCode, event);
		}

		//Log.d(TAG, "onKeyDown() end");

		return result;
	}

	/**
	 * @brief  初始化控件函数。
	 * @author 赵一
	 */
	private void _initView() {
		//Log.d(TAG, "_initView() start");

		//初始化消息接收器
		IntentFilter filter = new IntentFilter();
		filter.addAction(Define.Action.ACTION);
		registerReceiver(mReceiver, filter);

		//Log.d(TAG, "_initView() end");
	}

	/**
	 * @brief  初始化数据统计函数。
	 * @author 赵一
	 */
	private void _initStatistics() {
		//Log.d(TAG, "_initStatistics() start");

		//初始化友盟自动更新
		UmengSDK.autoUpdate(this);

		//初始化友盟错误报告
		UmengSDK.autoErrorReport(this);

		//初始化友盟在线配置
		UmengSDK.updateOnlineConfig(this);

		//Log.d(TAG, "_initStatistics() end");
	}

	/**
	 * @brief  初始化横幅广告函数。
	 * @author 赵一
	 */
	private void _initBanner() {
		//Log.d(TAG, "_initBanner() start");

		if (TapBurstFreeApplication.mIsAdBannerEnable == 1) {
			//初始化admob横幅广告
			GoogleAdmobSDK.createBanner(this, TapBurstFreeApplication.getAdmobUnitID(), Gravity.BOTTOM);
		}

		//Log.d(TAG, "_initBanner() end");
	}

	/**
	 * @brief  初始化推送广告函数。
	 * @author 赵一
	 */
	private void _initPush() {
		//Log.d(TAG, "_initPush() start");

		if (TapBurstFreeApplication.mIsAdPushEnable == 1) {
			//什么也不做
		}

		//Log.d(TAG, "_initPush() end");
	}

	/**
	 * @brief  退出函数。
	 * @author 赵一
	 */
	private void _exitApplication() {
		//Log.d(TAG, "_exitApplication() start");

		MessageDialog.show(this,
						   getString(R.string.exit_hint_title),
						   getString(R.string.exit_hint),
						   new DialogInterface.OnClickListener() {
							   @Override
							   public void onClick(DialogInterface dialog, int which) {
								   dialog.dismiss();
								   TapBurstFreeWindow.this.finish();
								   android.os.Process.killProcess(android.os.Process.myPid());
							   }
						   },
						   getString(R.string.yes),
						   new DialogInterface.OnClickListener() {
							   @Override
							   public void onClick(DialogInterface dialog, int which) {
								dialog.dismiss();
							   }
						   },
						   getString(R.string.no));

		//Log.d(TAG, "_exitApplication() end");
	}

	/**
	 * @brief 广播接收器。
	 */
	private BroadcastReceiver mReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			//Log.d(TAG, "BroadcastReceiver::onReceive() start");

			if (intent != null) {
				Bundle bundle = intent.getExtras();

				int type = bundle.getInt("TYPE");

				String url = bundle.getString("URL");
				switch (type) {
				case Define.MessageID.SHOW_LEADERBOARD:
					//显示排行榜
					if (Network.getConnectivityState(TapBurstFreeWindow.this) == true) {
						//排行榜名称
						String boardName = bundle.getString("BOARDNAME");
						if (ScoreloopSDK.showLeaderboards(TapBurstFreeWindow.this) != true) {
							Log.e(TAG, "BroadcastReceiver::onReceive(): show leaderboard failed!");
						}
					}
					else {
						//提示连接网络
						Toast.makeText(TapBurstFreeWindow.this,
									   R.string.can_not_connect_network,
									   Toast.LENGTH_SHORT).show();
					}
					break;

				case Define.MessageID.REPORT_SCORE:
					//提交得分
					if (Network.getConnectivityState(TapBurstFreeWindow.this) == true) {
						//排行榜名称
						String boardName = bundle.getString("BOARDNAME");
						//排行榜分值
						int score = bundle.getInt("SCORE");
						if (ScoreloopSDK.submit((double)score) != true) {
							Log.e(TAG, "BroadcastReceiver::onReceive(): submit score failed!");
						}
					}
					else {
						//提示连接网络
						Toast.makeText(TapBurstFreeWindow.this,
									   R.string.can_not_connect_network,
									   Toast.LENGTH_SHORT).show();
					}
					break;

				case Define.MessageID.SHARE_TEXT:
					//共享文本
					String text = bundle.getString("TEXT");
					if (Network.getConnectivityState(TapBurstFreeWindow.this) == true) {
						//分享文本
						if (Share.shareText(TapBurstFreeWindow.this,
											getString(R.string.app_name),
											getString(R.string.app_name),
											text) != true) {
							Log.e(TAG, "BroadcastReceiver::onReceive(): share text failed!");
						}
					}
					else {
						//提示连接网络
						Toast.makeText(TapBurstFreeWindow.this,
									   R.string.can_not_connect_network,
									   Toast.LENGTH_SHORT).show();
					}
					break;

				case Define.MessageID.WEB_BROWSER:
					//显示网页
					if (Network.getConnectivityState(TapBurstFreeWindow.this) == true) {
						if (Utility.startBrowser(TapBurstFreeWindow.this, url) != true) {
							Log.e(TAG, "BroadcastReceiver::onReceive(): start browser failed!");
						}
					}
					else {
						//提示连接网络
						Toast.makeText(TapBurstFreeWindow.this,
									   R.string.can_not_connect_network,
									   Toast.LENGTH_SHORT).show();
					}
					break;

				case Define.MessageID.GOTO_REVIEW:
					//提交评价
					if (Network.getConnectivityState(TapBurstFreeWindow.this) == true) {
						if (Comment.startGooglePlayComment(TapBurstFreeWindow.this, "com.happybluefin.tapburstfree") != true) {
							Log.e(TAG, "BroadcastReceiver::onReceive(): review failed!");
						}
					}
					else {
						//提示连接网络
						Toast.makeText(TapBurstFreeWindow.this,
									   R.string.can_not_connect_network,
									   Toast.LENGTH_SHORT).show();
					}
					break;

				case Define.MessageID.EXIT_APPLICATION:
					//退出
					_exitApplication();
					break;

				default:
					break;
				}
			}

			//Log.d(TAG, "BroadcastReceiver::onReceive() end");
		}
	};

	/**
	 * @brief 日志标记。
	 */
	private static final String TAG = "TapBurstFreeWindow";
}
